function isEmpty(checkString){
    return !checkString || checkString.trim().length === 0
}

function isNumber(numberString) {
    var pattern = /^\d+$/;
    return pattern.test(numberString)
}

function isInteger(intString) {
    var pattern = /^[-+]?\d*$/;
    return pattern.test(intString)
}

function isEnglishLetter(letterString){
    var pattern = /^[a-zA-Z]+$/;
    return pattern.test(letterString)
}

function isChineseCharacter(characterString){
    var pattern = /^[\u4E00-\u9FA5]{1,6}$/;
    return pattern.test(characterString)
}

function isEmail(email) {
    var pattern = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
    return pattern.test(email);
}

function isPhoneNumber(phoneNumber){
    var pattern = /^1[3456789]\d{9}$/;
    return pattern.test(phoneNumber);
}

function isIDNumber(idNumber){
    var pattern = /(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
    if(pattern.test(idNumber)) {
        //身份证第7到14位是出生日期
        var year = idNumber.substring(6, 10);
        var current_year = new Date().getFullYear();
        if ((year > 1949) && (year < current_year)) {
            var month = idNumber.substring(10, 12);
            var day = idNumber.substring(12, 14);
            if (!isNaN(Date.parse(year + '/' + month + '/' + day))) {
                return true;
            }
        }
    }
    return false;
}

function isAcademicCertNumber(certNumber) {
    if(isForeignDegreeCertNumber(certNumber))
        return true;

    if(isReplacedDegreeCertNumber(certNumber))
        return true;

    var pattern = /(^[156789]\d{16}$)|(^[156789]\d{17}$)/;
    //var pattern = /(^\d{17}$)|(^\d{18}$)/;
    if(pattern.test(certNumber)){
        var firstNumber = parseInt(certNumber.substring(0,1));
        if(firstNumber === 6){
            //自学考试文凭的第二位是学历层次
            var secondNumber = parseInt(certNumber.substring(1,2));
            if( (secondNumber > 0) && (secondNumber < 7) ){
                return true;
            }
        }else{
            //学历证第7至10位为年份
            var year = certNumber.substring(6,10);
            var current_year=new Date().getFullYear();
            if((year > 1990) && (year <= current_year)){
                return true;
            }
        }
    }
    return false;
}

function isDegreeMatchCertNumber(degreeLevel, certNumber){
    if(isForeignDegreeCertNumber(certNumber))
        return true;

    if(isReplacedDegreeCertNumber(certNumber))
        return true;

    degreeLevel = parseInt(degreeLevel);
    var pattern = /(^[156789]\d{16}$)|(^[156789]\d{17}$)/;
    if(pattern.test(certNumber)){
        //非自考学历证第11至第12位是学历级别，自考学历证第2位是级别
        var degreeLevelFromCert = 0;
        var firstNumber = parseInt(certNumber.substring(0,1));
        if(firstNumber === 6){
            degreeLevelFromCert = parseInt(certNumber.substring(1,2));
        }else{
            degreeLevelFromCert = certNumber.substring(10,12);
        }

        degreeLevelFromCert = parseInt(degreeLevelFromCert);
        //站内的学历级别和官方有差别，要区分对比
        if(degreeLevel === 1 || degreeLevel === 2){
            return degreeLevel === degreeLevelFromCert
        }else if(degreeLevel === 3){
            return degreeLevelFromCert === 4 || degreeLevelFromCert === 5;
        }else if(degreeLevel === 4){
            return degreeLevelFromCert === 6;
        }
    }

    return false;
}

function isReplacedDegreeCertNumber(certNumber){
    certNumber = certNumber.toString().toLowerCase().trim();
    return certNumber.indexOf('b') !== -1;
}

function isForeignDegreeCertNumber(certNumber) {
    certNumber = certNumber.toString().trim();
    return certNumber.indexOf('教留服') !== -1;
}

function isWeixinID(weixinID) {
    var pattern = /^[a-zA-Z0-9_-]{5,20}$/;
    return pattern.test(weixinID);
}

function isPasswordTooWeak(passwordStr) {
    var pattern = /^(?:\d+|[a-zA-Z]+)$/;
    var pattern2 = /^\S{8,20}$/;
    return pattern.test(passwordStr) || !pattern2.test(passwordStr);
}

//format is yyyy-mm-dd
function isDate(dateStr) {
    var rxDatePattern = /^(\d{4})(\/|-)(\d{1,2})(\/|-)(\d{1,2})$/;
    return rxDatePattern.test(dateStr)
}

function isIntNumber(strNumber) {
    return $.isNumeric(strNumber) && (parseInt(strNumber) == strNumber);
}

function verifyPhoneCode(phoneCode){
    if(!(phoneCode && phoneCode !== '')){
        return false;
    }else if(phoneCode.length !== 6){
        return false;
    }

    return true;
}

function getUserPhoneVerifyCode(){
    getPhoneVerifyCode($(this), true);
}

function getGuestPhoneVerifyCode() {
    getPhoneVerifyCode($(this), false);
}

function getPhoneVerifyCode(verifyCodeLink, for_user){
    var userPhoneNumber = $('#userPhone').val();

    var signupInfoContainer = $("#signupInfo");
    signupInfoContainer.attr("style", "color:#ff6666;");
    signupInfoContainer.hide();
    if(!isPhoneNumber(userPhoneNumber)){
        signupInfoContainer.text("请输入正确的手机号");
        signupInfoContainer.show();
        return false;
    }

    var csrf_token = $('[name=csrfmiddlewaretoken]').val();

    $.post('/get-phone-verify-code/',
        {userPhone: userPhoneNumber, forUser: for_user, csrfmiddlewaretoken: csrf_token},
        function (data){
            var signupInfoContainer = $("#signupInfo");
            signupInfoContainer.hide();

            if(data.result === 0){

                signupInfoContainer.text(data.msg);
                signupInfoContainer.attr("style", "color:#67b168;");
                signupInfoContainer.show();

                verifyCodeLink.off('click');
                var clickTime = new Date().getTime();
                var Timer = setInterval(function(){
                    var nowTime = new Date().getTime();
                    var second  = Math.ceil(60-(nowTime-clickTime)/1000);
                    if(second>0){
                        verifyCodeLink.text(second+"秒后重新获取");
                    }else{
                        clearInterval(Timer);
                        verifyCodeLink.text("获取验证码");
                        verifyCodeLink.on('click',getPhoneVerifyCode);
                    }
                },1000);
            }else{
                signupInfoContainer.text(data.msg);
                signupInfoContainer.attr("style", "color:#ff6666;");
                signupInfoContainer.show();
            }
        }
    );
}

function backToListItemClick(curr_page) {
    Cookies.set('mxdx_list_curr_page', curr_page);
    return true;
}

function show_more_content(btn) {
    $(btn).parent('.short-content').hide();
    $(btn).parent().parent().find('.long-content').show();
}

function show_less_content(btn) {
    $(btn).parent('.long-content').hide();
    $(btn).parent().parent().find('.short-content').show();
}

function copy_to_clipboard(elementId) {
    var element = document.getElementById(elementId);
    var $temp = $("<input>");
    $("body").append($temp);
    $temp.val($(element).text()).select();
    document.execCommand("copy");
    $temp.remove();
}

function copy_text(element, copyBtn) {
    //Before we copy, we are going to select the text.
    var text = document.getElementById(element);
    var selection = window.getSelection();
    var range = document.createRange();
    range.selectNodeContents(text);
    selection.removeAllRanges();
    selection.addRange(range);
    //add to clipboard.
    document.execCommand('copy');
    copyBtn.innerHTML = '已拷贝';
}
